package com.eigaland.change_machine.business.movelistfullscreen

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.eigaland.change_machine.BR
import com.eigaland.change_machine.R
import com.eigaland.change_machine.adapter.BaseBindAdapter
import com.eigaland.change_machine.adapter.ViewPagerViewAdapter
import com.eigaland.change_machine.business.movielist.MovieListItemVM
import com.eigaland.change_machine.business.show.ShowListFragment
import com.eigaland.change_machine.util.ProgressDialogManager
import com.eigland.eigaland_lib.base.BaseLazyFragment
import com.google.gson.Gson
import kotlinx.android.synthetic.main.fragment_movie_list_full_screen.*
import kotlinx.android.synthetic.main.include_just_recyeleview.view.*
import me.yokeyword.fragmentation.anim.DefaultHorizontalAnimator
import me.yokeyword.fragmentation.anim.FragmentAnimator
import net.lucode.hackware.magicindicator.ViewPagerHelper
import java.util.logging.Logger

public class MovieListFullScreenFragment :BaseLazyFragment<MovieListFullScreenFragmentVM,com.eigaland.change_machine.databinding.FragmentMovieListFullScreenBinding>(){


    override fun initVM() {

            vm?.initData()
            vm?.movieListItems?.observe(this, Observer {
                var list = ArrayList<View>()

                for (index in 0 until it.size) {
                    var view = LayoutInflater.from(requireContext()).inflate(R.layout.include_just_recyeleview,null)
                    view.recyclerview.layoutManager = GridLayoutManager(requireContext(),6)

                    var adapter = BaseBindAdapter<MovieListItemVM>(R.layout.item_movie_list_data_full_screen, BR.movieListItemVM);
                    adapter.setOnItemClickListener { _, _, position ->

                        var frg = ShowListFragment()
                        var movieId = it[index][position].movieId
                        var bundle = Bundle()
                        bundle.putString("movieId",movieId)
                        frg.arguments = bundle
                        supportDelegate.start(frg)

                    }
                    adapter.addData(it[index])
                    view.recyclerview.adapter =  adapter
                    list.add(view)
                }


                binding?.fullscreenIndicator?.setData(list.size,0)
                binding?.fullscreenIndicator?.setUpViewPager(view_pager)
                view_pager.adapter = ViewPagerViewAdapter(list)


            })
        vm?.progressLoadingStatus?.observe(this, Observer {
            if(it) ProgressDialogManager.showDialog(requireContext())
             else ProgressDialogManager.dismissDialog(requireContext())
        })
    }



    override fun setUpViewData(view: View) {
        vm = MovieListFullScreenFragmentVM()



        binding = DataBindingUtil.bind(view)
        binding?.let {
            it.lifecycleOwner = this
            it.fullscreenVM = vm
        }

    }

    override fun onCreateFragmentAnimator(): FragmentAnimator {
        return DefaultHorizontalAnimator()
    }

    override fun setLayoutId() = R.layout.fragment_movie_list_full_screen

}